﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SalaryChannel.SCUserService;

namespace SalaryChannel
{
    public partial class PwdControl : UserControl
    {
       // static SalaryChannel.DCWebService.DCWebServiceSoapClient dclient = new SalaryChannel.DCWebService.DCWebServiceSoapClient();
        public PwdControl()
        {
            InitializeComponent();

            ServiceInstance.UserClient.ChangePwdCompleted += new EventHandler<ChangePwdCompletedEventArgs>(dclient_ChangePwdCompleted);
            ServiceInstance.UserClient.UpdateTelEmailCompleted += new EventHandler<UpdateTelEmailCompletedEventArgs>(UserClient_UpdateTelEmailCompleted);
        }

       

        public void Initiallize()
        {
            this.OldPwd.Password = string.Empty;
            this.NewPwd.Password = string.Empty;
            this.ConfirmPwd.Password = string.Empty;
            this.UserIDTB.Text = App.CurrentUser.UserID;
            this.UserIDTB.IsReadOnly = !App.IsAuth(6);
            this.OldPwd.IsEnabled = !App.IsAuth(6);
            this.TelTB.Text = App.CurrentUser.Tel==null ? string.Empty:App.CurrentUser.Tel;
            this.EmailTB.Text = App.CurrentUser.Email == null? string.Empty:App.CurrentUser.Email;

            this.oldPwdTb.Visibility = !App.IsAuth(6) ? Visibility.Visible : Visibility.Collapsed;
            this.OldPwd.Visibility = !App.IsAuth(6) ? Visibility.Visible : Visibility.Collapsed;


        }

        void dclient_ChangePwdCompleted(object sender,ChangePwdCompletedEventArgs e)
        {
            string msg = string.Empty;
            if (e.Result == 0)
            {
                msg = Properties.Resource.Succeeded;
                OldPwd.Password = string.Empty;
                NewPwd.Password = string.Empty;
                ConfirmPwd.Password = string.Empty;
            }
            else if (e.Result == 1)
            {
                msg = Properties.Resource.OldPwdWrong;
            }
            else if (e.Result == 2)
            {
                msg = Properties.Resource.TryAgain;
            }
            else if (e.Result == 3)
            {
                msg = Properties.Resource.IDNotExists;
            }
            App.MessageBox.ShowDialog(msg, Title.Information);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (!App.IsAuth(6))
            {
                if (string.IsNullOrEmpty(NewPwd.Password)
                    || string.IsNullOrEmpty(ConfirmPwd.Password)
                    || NewPwd.Password != ConfirmPwd.Password)
                {
                    App.MessageBox.ShowDialog(Properties.Resource.PwdInputWarining, Title.Warning);
                    return;
                }
            }

            if (NewPwd.Password==null || NewPwd.Password.Length < 8)
            {
                App.MessageBox.ShowDialog("密码长度不能小于8位", Title.Warning);
                return;
            }
            try
            {
                ServiceInstance.UserClient.ChangePwdAsync(this.UserIDTB.Text, OldPwd.Password, NewPwd.Password, App.IsAuth(6),App.CurrentUser.UserID);
            }
            catch
            {
                App.MessageBox.ShowDialog(Properties.Resource.ConnectServerFailed, Title.Error);
            }
        }

        private void UpdateUserBtn_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(TelTB.Text)
                || TelTB.Text.Length != 11)
            {
                App.MessageBox.ShowDialog("请正确填写手机号");
                return;
            }
            System.Text.RegularExpressions.Regex emailRe = new System.Text.RegularExpressions.Regex(DataHelper.EmailRegexString);
            if (!emailRe.IsMatch(EmailTB.Text))
            {
                App.MessageBox.ShowDialog("请正确填写邮箱");
                return;
            }
            try
            {
                ServiceInstance.UserClient.UpdateTelEmailAsync(this.TelTB.Text, EmailTB.Text, App.CurrentUser.UserID);
            }
            catch 
            {
                App.MessageBox.ShowDialog("连接服务器失败");
            }

        }
        void UserClient_UpdateTelEmailCompleted(object sender, UpdateTelEmailCompletedEventArgs e)
        {
            try
            {
                if (e.Result)
                {
                    App.MessageBox.ShowDialog("修改成功");
                }
                else
                {
                    App.MessageBox.ShowDialog("修改失败，请稍后再试");
                }
            }
            catch 
            {
                App.MessageBox.ShowDialog("修改失败，请稍后再试");
            }
        }
    }
}
